package net.mcfire.aliyunplugin;

import com.aliyun.openservices.log.common.LogItem;

public class TopicLog {

    public final String topic;

    protected TopicLog(String topic) {
        this.topic = topic;
    }

    public void info(String text) {
        log("INFO", text);
    }

    public void debug(String text) {
        log("DEBUG", text);
    }

    public void error(String text, Throwable throwable) {
        LogItem item = new LogItem();
        item.PushBack("text", text);
        StringBuilder sb = new StringBuilder();
        for(StackTraceElement elem : throwable.getStackTrace()) {
            sb.append(elem.toString());
            sb.append("\n");
        }
        item.PushBack("stacktrace", sb.toString());
        AliyunPlugin.sendLog(topic, "ERROR", item);
    }

    public void error(String text) {
        error(text, new Exception());
    }

    public void log(String level, String text) {
        LogItem item = new LogItem();
        item.PushBack("text", text);
        AliyunPlugin.sendLog(topic, "INFO", item);
    }

}
